package com.aube.rvi.module;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.aube.rvi.OnVideoPlayerListener;

/**
 * Created by huyaonan on 16/10/21.
 */
public abstract class DLLayout extends FrameLayout implements OnVideoPlayerListener {
    private DLPluginPackage mPluginPackage;
    protected Context context;

    public DLLayout(Context context) {
        this(context, null);
    }
    public DLLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public DLLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void injectPluginPackage(DLPluginPackage mPluginPackage) {
        this.mPluginPackage = mPluginPackage;

        initViews();
    }

    public abstract void initViews();

    public abstract void fillData(String data);

    public boolean onBackPressed(){return false;}

    public void onResume() {}

    public void attachViews(View... view){}

    public Resources getResources() {
        if(mPluginPackage == null)
            return super.getResources();
        return mPluginPackage.resources;
    }

    public DLPluginPackage getDLPluginPackage() {
        return mPluginPackage;
    }

    public View inflate(int id, ViewGroup root, boolean attachToRoot) {

        final XmlResourceParser parser = getResources().getLayout(id);
        try {
            return LayoutInflater.from(context).inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

    public void setImgWithRes(ImageView img, int res) {
        img.setImageDrawable(getResources().getDrawable(res));
    }

    public void setBgWithRes(View view, int res) {
        view.setBackgroundDrawable(getResources().getDrawable(res));
    }

}